﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;

using WinnerCMS.Common;
using WinnerCMS.Model;
using System.Data.Entity;

namespace WinnerCMS.Data
{
    /// <summary>
    /// 投票选项
    /// </summary>
    public class VoteChoiceDal
    {
        #region  添加数据
        /// <summary>
        /// 添加数据
        /// </summary>
        public void Add(VoteChoice model)
        {
            using (Entities db = new Entities())
            {
                db.VoteChoices.Add(model);
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  修改数据
        /// <summary>
        /// 修改
        /// </summary>
        public void Edit(VoteChoice model)
        {
            using (Entities db = new Entities())
            {
                db.VoteChoices.Attach(model);
                db.Entry(model).State = EntityState.Modified;
                db.SaveChangesAsync().Wait();
            }
        }
        #endregion

        #region  投票
        /// <summary>
        /// 投票
        /// </summary>
        public void VoteThis(Vote info)
        {
            using (Entities db = new Entities())
            {
                var q = db.VoteChoices.Where(x => info.VoteChoices.Any(y=>y.Id==x.Id));
                foreach (var v in q)
                {
                    v.VoteNum = v.VoteNum + 1;
                }
                db.SaveChangesAsync().Wait();
            }

        }
        #endregion

        #region  删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            using (Entities db = new Entities())
            {
                var q = db.VoteChoices.FirstOrDefault(x => x.Id == id);
                db.VoteChoices.Remove(q);
                db.SaveChangesAsync().Wait();
            }
        }

        #endregion

        #region  加载数据
        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public VoteChoice GetInfo(int id)
        {
            using (Entities db = new Entities())
            {
                return db.VoteChoices.FirstOrDefault(x => x.Id == id);

            }
        }
        #endregion

        #region  获得投票的项目列表
        /// <summary>
        /// 获得投票的项目列表
        /// </summary>
        /// <param name="voteId"></param>
        /// <returns></returns>
        public IList<VoteChoice> ForVoteLoad(int voteId)
        {
            using (Entities db = new Entities())
            {
                return db.VoteChoices.Where(x => x.VoteId == voteId).OrderBy(x => x.OrderId).ToList();
            }
        }
        #endregion

        #region 取得投票结果
        /// <summary>
        /// 取得投票结果
        /// </summary>
        /// <param name="voteId"></param>
        /// <returns></returns>
        public IList<VoteChoice> GetResult(int voteId)
        {
            using (Entities db = new Entities())
            {
                return db.VoteChoices.Where(x => x.VoteId == voteId).OrderBy(x => x.OrderId).ToList();
            }
        }

        #endregion
    }
}